/* eslint-disable no-console */
export class Logger {
	public static debug(message: any, error?: Error) {
		console.debug(Logger.getFormatedMessage('[DEBUG]', message, error))
	}

	public static error(message: any, error?: Error) {
		console.debug(Logger.getFormatedMessage('[ERROR]', message, error))
	}

	private static format(value: number, length: number, padEnd?: boolean): string {
		let result = value.toString()
		if (result.length < length) {
			if (padEnd) {
				result = result.padEnd(length, '0')
			} else {
				result = result.padStart(length, '0')
			}
		} else if (result.length > length) {
			result = result.substr(0, length)
		}

		return result
	}

	private static getFormatedMessage(type: string, message: any, error?: Error): string {
		const d = new Date()
		const year = Logger.format(d.getFullYear(), 4)
		const month = Logger.format(d.getMonth() + 1, 2)
		const date = Logger.format(d.getDate(), 2)
		const hour = Logger.format(d.getHours(), 2)
		const minute = Logger.format(d.getMinutes(), 2)
		const second = Logger.format(d.getSeconds(), 2)
		const millisecond = Logger.format(d.getMilliseconds(), 3, true)
		const time = `${year}-${month}-${date} ${hour}:${minute}:${second}.${millisecond}`
		let text = ''
		if (this.isObject(message)) {
			text = JSON.stringify(message)
		} else {
			text = message
		}
		if (error) {
			text = `${text}\t${error.message}`
			console.log(error)
		}
		return `${time} ${type}\t${text}`
	}

	public static info(message: any, error?: Error) {
		console.info(Logger.getFormatedMessage('[INFO ]', message, error))
	}

	private static isObject(value: any) {
		return value !== null && typeof value === 'object'
	}

	public static warn(message: any, error?: Error) {
		console.warn(Logger.getFormatedMessage('[WARN ]', message, error))
	}
}
